package org.etherframework.tests.features.contextual
{
	import org.etherframework.core.EtherConfig;
	import org.etherframework.tests.features.contextual.configs.HierarchicalConfig;
	import org.etherframework.tests.features.contextual.contexts.AnyContext;
	import org.etherframework.tests.features.contextual.mediators.ChildModuleMediator;
	import org.etherframework.tests.features.contextual.mediators.ModuleMediator;
	import org.etherframework.tests.features.contextual.mediators.NonContextMediator;
	import org.etherframework.tests.features.contextual.mediators.TopModuleMediator;
	import org.etherframework.tests.features.contextual.views.NonContextMediatorView;
	import org.etherframework.tests.features.contextual.views.TrivialView;
	import org.etherframework.tests.utils.ModuleHierarchy;
	import org.etherframework.tests.utils.TestUtility;
	import org.fluint.uiImpersonation.UIImpersonator;
	import org.hamcrest.assertThat;
	import org.hamcrest.object.instanceOf;
	import org.hamcrest.object.notNullValue;

	public class when_using_contextual_mediators
	{	
		private static var _testUtility:TestUtility = new TestUtility();
		private static var _hiearchy:ModuleHierarchy;
		
		[BeforeClass]
		public static function setUpBeforeClass():void
		{
			const configs:Vector.<EtherConfig> = new Vector.<EtherConfig>();
			configs.push(new HierarchicalConfig(), new HierarchicalConfig(), new HierarchicalConfig());
			_hiearchy = _testUtility.createHierarchy(configs);
			UIImpersonator.addChild(_hiearchy.module.view);
			
			_hiearchy.module.mediators.addMediator(NonContextMediatorView, NonContextMediator);
			_hiearchy.module.mediators.contextual(new AnyContext()).addMediator(TrivialView, TopModuleMediator);
			_hiearchy.next().module.mediators.contextual(new AnyContext()).addMediator(TrivialView, ModuleMediator);
			_hiearchy.next().next().module.mediators.contextual(new AnyContext()).addMediator(TrivialView, ChildModuleMediator);
		}
		
		[AfterClass]
		public static function tearDownAfterClass():void
		{
			UIImpersonator.removeAllChildren();
			_testUtility.dispose();
		}
		
		[Test(order=1)]
		public function it_should_be_resolved():void
		{
			var error:Error;
			
			assertThat(_hiearchy.module.mediators.createMediator(new NonContextMediatorView()), instanceOf(NonContextMediator));
			assertThat(_hiearchy.module.mediators.contextual(new AnyContext()).createMediator(new TrivialView()), instanceOf(TopModuleMediator));
			assertThat(_hiearchy.next().module.mediators.contextual(new AnyContext()).createMediator(new TrivialView()), instanceOf(ModuleMediator));
			assertThat(_hiearchy.next().next().module.mediators.contextual(new AnyContext()).createMediator(new TrivialView()), instanceOf(ChildModuleMediator));
			
			
			try {
				_hiearchy.next().module.mediators.createMediator(new TrivialView());
			} catch (e:Error) {error = e;}
			assertThat(error, notNullValue());
			error = null;
			
			try {
				_hiearchy.next().next().module.mediators.createMediator(new TrivialView());
			} catch (e:Error) {error = e;}
			assertThat(error, notNullValue());
		}
		
		[Test(order=2)]
		public function it_should_be_resolved_in_default_context_through_custom():void
		{
			assertThat(_hiearchy.module.ether.contextual(new AnyContext()).mediators.createMediator(new NonContextMediatorView()), instanceOf(NonContextMediator));
			assertThat(_hiearchy.next().module.ether.contextual(new AnyContext()).mediators.createMediator(new NonContextMediatorView()), instanceOf(NonContextMediator));
			assertThat(_hiearchy.next().next().module.ether.contextual(new AnyContext()).mediators.createMediator(new NonContextMediatorView()), instanceOf(NonContextMediator));
		}
	}
}